// pages/orderaffirm/orderaffirm.js
var app = getApp();
var com = require('../../common.js');
Page({
  data: {
    confirmedList: '',
    confirmedPage: '',
    statusd: 1
  },
  onLoad: function(options) {
    var _this = this
    _this.accept_accept()
    setTimeout(() => {
      _this.setData({
        statusd: 0
      })
    }, 1500)
  },
  onReady: function() {},
  onShow: function() {},
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {
    var _this = this
    _this.showList(_this.data.active)
  },
  //点击加载数据
  showList(e) {
    var _this = this,
      list = _this.data.confirmedList
    let orderPage = _this.data.confirmedPage
    if (!orderPage) {
      console.log('没有更多数据了')
      wx.showToast({
        title: '没有更多订单了',
        icon: 'none'
      })
    } else {
      com.link(orderPage, {}, function(rep) {
        console.log(rep)
        for (let i in rep.data.data) {
          list.push(rep.data.data[i])
        }
        _this.setData({
          confirmedList: list,
          confirmedPage: rep.data.links.next
        })
      }, "GET")
    }
  },
  onShareAppMessage: function() {},
  //获取待确认订单
  accept_accept() {
    var _this = this,
      url = app.link.accept_accept,
      data = {}
    com.link(url, data, function(rep) {
      console.log(rep);
      _this.setData({
        confirmedList: rep.data.data,
        confirmedPage: rep.data.links.next
      })
    }, "GET")
  },
  //确认订单
  onOrderSuccess(e) {
    var _this = this,
      id = e.currentTarget.id,
      url = app.link.acceptOrder + id + '.st',
      data = {}
    wx.showModal({
      title: '温馨提示',
      content: '确认接单吗？',
      success(e) {
        if (e.confirm) {
          com.link(url, data, function(rep) {
            console.log(rep)
            if (rep.data.state) {
              wx.showToast({
                title: '接单成功',
                icon: 'success'
              })
              setTimeout(() => {
                _this.accept_accept()
              }, 1500)
            } else {
              wx.showToast({
                title: rep.data.message,
                icon: 'none'
              })
            }
          }, "GET")
        }
      }
    })
  },
})